home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 43 / Amiga Format CD43 (1999)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1999-09].iso / -serious- / hardware / camedia / camprg.c < prev    next >
C/C++ Source or Header  |  1999-06-14  |  17KB  |  678 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. #include <dos/dos.h>
  7. #include <exec/exec.h>
  8. #include <devices/serial.h>
  9. #include <jpeg/jpeg.h>
  10. #include <cybergraphics/cybergraphics.h>
  11. #include <intuition/intuition.h>
  12. #include <libraries/asl.h>
  13.  
  14. #include <clib/exec_protos.h>
  15. #include <clib/dos_protos.h>
  16. #include <clib/alib_protos.h>
  17. #include <clib/jpeg_protos.h>
  18. #include <clib/cybergraphics_protos.h>
  19. #include <clib/intuition_protos.h>
  20. #include <clib/asl_protos.h>
  21.  
  22. #include <pragma/dos_lib.h>
  23. #include <pragma/exec_lib.h>
  24. #include <pragma/jpeg_lib.h>
  25. #include <pragma/cybergraphics_lib.h>
  26. #include <pragma/intuition_lib.h>
  27. #include <pragma/asl_lib.h>
  28.  
  29. #include "Camedia.h"
  30.  
  31. struct Library *JpegBase, *CyberGfxBase;
  32.  
  33. void info_f(enum Camera_Speed cs, char* error_msg);
  34. void preview_f(enum Camera_Speed cs, char* error_msg);
  35. void do_f(int user_min, int user_max, char *name, long speed, Camera_Speed cs, int action, char* error_msg);
  36.  
  37. void main(int argc, char *argv[]) {
  38.     enum arg {
  39.         NONE = -2,
  40.         WRONG_MODE = -1,
  41.         DEVICE = 0, 
  42.         UNIT, 
  43.         SPEED, 
  44.         INFO,
  45.         PREVIEW,
  46.         TAKE_PICTURE,
  47.         RETR,
  48.         THUMBNAILS,
  49.         DEL,
  50.         FROM,
  51.         TO,
  52.         NAME};
  53.         
  54.     char *input_mask = "DEVICE/K,UNIT/N,SPEED/N,INFO/S,PREVIEW/S,TAKE_PICTURE/S,RETR/S,THUMBNAILS/S,DEL/S,FROM/N,TO/N,NAME/K";
  55.     enum Camera_Speed cs;
  56.     long speed=0;
  57.     int from, to, i, unit, modus = NONE;
  58.     LONG array[12];
  59.     struct RDArgs *rdargs;
  60.     char devicename[256], picture_name[256], error_msg[256];
  61.     
  62.     *error_msg=0;
  63.  
  64.     for (i=0; i<12; i++) array[i]=0;
  65.     rdargs=ReadArgs(input_mask, array, NULL);
  66.     if (rdargs==NULL) {
  67.         printf("An error occured in ReadArgs(%s, array, NULL);\n", input_mask);
  68.         exit(0);
  69.     }
  70.     
  71.     /* DEVICE/K */
  72.     if (array[DEVICE]==0) strcpy(devicename, "serial.device");
  73.     else strcpy(devicename, (char *) array[DEVICE]);
  74.     
  75.     /* UNIT/N */
  76.     if (array[UNIT]!=0) unit=*((long *) array[UNIT]);
  77.     else unit=0;
  78.     
  79.     /* SPEED/N */
  80.     if (array[SPEED]==0) {
  81.         speed=19200;
  82.         cs=CAMERA_SPEED_19200;
  83.     } else {
  84.         speed=*((long *) array[SPEED]);
  85.     
  86.         switch (speed) {
  87.             case 9600:
  88.                 cs=CAMERA_SPEED_9600;
  89.                 break;
  90.             case 19200:
  91.                 cs=CAMERA_SPEED_19200;
  92.                 break;
  93.             case 38400:
  94.                 cs=CAMERA_SPEED_38400;
  95.                 break;            
  96.             case 57600:
  97.                 cs=CAMERA_SPEED_57600;
  98.                 break;
  99.             case 115200:
  100.                 cs=CAMERA_SPEED_115200;
  101.                 break;
  102.             default:
  103.                 // cs=CAMERA_SPEED_Unknown;
  104.                 printf("Unknown speed, must be either 9600, 19200, 38400, 57600 or 115200\n");
  105.                 exit(0);
  106.         }
  107.     }
  108.         
  109.     /* INFO/S */ 
  110.     if ((int) array[INFO]) modus=INFO;
  111.     
  112.     /* PREVIEW/S */
  113.     if ((int) array[PREVIEW]) {
  114.         if (modus!=NONE) {
  115.             printf("Choose exactly one mode!\n");
  116.             modus=WRONG_MODE;
  117.         } else
  118.             modus=PREVIEW;
  119.     }
  120.     
  121.     /* TAKE_PICTURE/S */
  122.     if ((int) array[TAKE_PICTURE]) {
  123.         if (modus!=NONE) {
  124.             printf("Choose exactly one mode!\n");
  125.             modus=WRONG_MODE;
  126.         } else 
  127.             modus=TAKE_PICTURE;
  128.     }
  129.     
  130.     /* RETR/S */
  131.     if ((int) array[RETR]) {
  132.         if (modus!=NONE) {
  133.             printf("Choose exactly one mode!\n");
  134.             modus=WRONG_MODE;
  135.         } else {
  136.             modus=RETR;
  137.             printf("get Frames ");
  138.         }
  139.     }
  140.     
  141.     /* THUMBNAILS/S */
  142.     if ((int) array[THUMBNAILS]) {
  143.         if (modus!=NONE) {
  144.             printf("Choose exactly one mode!\n");
  145.             modus=WRONG_MODE;
  146.         } else {
  147.             modus=THUMBNAILS;
  148.             printf("get Thumbnails ");
  149.         }
  150.     }
  151.     
  152.     /* DEL/S */
  153.     if ((int) array[DEL]) {
  154.         if (modus!=NONE) {
  155.             printf("Choose exactly one mode!\n");
  156.             modus=WRONG_MODE;
  157.         } else {
  158.             modus=DEL;
  159.             printf("delete Frames ");
  160.         }
  161.     }
  162.     
  163.     printf("start\n");
  164.     if ((modus==RETR) || (modus==THUMBNAILS) || (modus==DEL)) {
  165.         /* FROM/N */
  166.         if (array[FROM]!=0) from=(int) *((long *) array[FROM]);
  167.         else from=0;
  168.     
  169.         /* TO/N */
  170.         if (array[TO]!=0) to=(int) *((long *) array[TO]);
  171.         else to=0;
  172.     
  173.         /* NAME/K */
  174.         if (array[NAME]==0) strcpy(picture_name, "picture");
  175.         else strcpy(picture_name, (char *) array[NAME]);
  176.         
  177.         printf("from %d to %d with name %s\n", from, to, picture_name);
  178.     }
  179.     printf("end\n");
  180.     
  181.     FreeArgs(rdargs);
  182.     
  183.     if (modus==NONE) {
  184.         printf("Choose one mode, which is Either INFO, PREVIEW, RETR, THUMBNAILS or DEL\n");
  185.         exit(0);
  186.     }
  187.     
  188.     bootstrap(devicename, unit, error_msg);
  189.     if (*error_msg!=0) {
  190.         printf("There was an error in bootstrap: %s\n", error_msg);
  191.         exit(0);
  192.     }
  193.     
  194.     switch (modus){
  195.         
  196.         case THUMBNAILS:
  197.             if (from==0) from=1;
  198.             if (to==0) to=255;
  199.             do_f(from, to, picture_name, speed, cs, 0, error_msg);
  200.             break;
  201.         
  202.         case RETR:
  203.             if (from==0) from=1;
  204.             if (to==0) to=255;
  205.             do_f(from, to, picture_name, speed, cs, 1, error_msg);
  206.             break;
  207.         
  208.         case DEL:
  209.             if (from==0) from=1;
  210.             if (to==0) to=255;
  211.             printf("from=%d\n", from);
  212.             do_f(from, to, picture_name, speed, cs, 2, error_msg);
  213.             break;
  214.             
  215.         case TAKE_PICTURE:
  216.             // for this short code, I don't define a seperate function:)
  217.             init(cs, error_msg);
  218.             if (*error_msg==0) {
  219.                 TakeAction(AC_Take_Picture, 0, error_msg);
  220.                 // overwrite timeout_error, which is ok in this single case
  221.                 *error_msg=0;
  222.             }                
  223.             break;
  224.         
  225.         case PREVIEW:
  226.             preview_f(cs, error_msg);
  227.             break;
  228.             
  229.         case INFO:
  230.             info_f(cs, error_msg);
  231.             break;
  232.             
  233.         default:
  234.             printf("ops, there is somthing wrong with the mode\n");
  235.     }
  236.     printf("Well, all done\n");
  237.     
  238.     if (*error_msg!=0) {
  239.         printf("There was an error: %s\n", error_msg);
  240.     }
  241.     clean(100);
  242. }
  243.  
  244.  
  245. void info_f(enum Camera_Speed cs, char* error_msg) {
  246.     Resolution *re=NULL;
  247.     time_t *c=NULL, *TimeDate=NULL;
  248.     long *ss=NULL;
  249.     Aperture *a=NULL;
  250.     Color_Mode *cm=NULL;
  251.     Flash_Mode *fm=NULL;
  252.     int *noft=NULL, *nofl=NULL, *bc=NULL, *aoht=NULL, *asoft=NULL, *lb=NULL, *lat=NULL;
  253.     int *AEL_WBL=NULL, *Fisheye=NULL, *Wide=NULL, *Digital_Zoom=NULL, *BAW=NULL;
  254.     int *LCD_Brightness=NULL, *Frames_Taken=NULL,*ProtectionState=NULL, *psof=NULL;
  255.     Camera_Speed *ccs=NULL;
  256.     Bright_Contrast *brc=NULL;
  257.     White_Balance *wb=NULL;
  258.     Date_Format *df=NULL;
  259.     Exp_Meter *em=NULL;
  260.     long *aml=NULL, *locf=NULL, *loct=NULL, *AudioDataLength=NULL;
  261.     Lens_Mode *lm=NULL;
  262.     int i, max;
  263.     char buf[1024];
  264.  
  265.     init(cs, error_msg);
  266.     if (*error_msg!=0) return;
  267.     
  268.     Get_Resolution(re, error_msg);
  269.     if (*error_msg!=0) return;
  270.     printf("Resolution=%s\n", dump_Resolution(*re));
  271.  
  272.     Get_Clock(c, error_msg);
  273.     if (*error_msg!=0) return;
  274.     printf("Clock=%s\n", asctime(localtime(c)));
  275.         
  276.     Get_Shutter_Speed(ss, error_msg);
  277.     if (*error_msg!=0) return;
  278.     printf("Shutter_Speed=%d\n", *ss);
  279.         
  280.     /* Get_Aperture(a, error_msg);
  281.      if (*error_msg!=0) return;
  282.      printf("Aperture=%s\n", dump_Aperture(*a));
  283.     */
  284.          
  285.     Get_Color_Mode(cm, error_msg);
  286.     if (*error_msg!=0) return;
  287.     printf("Color_Mode=%s\n", dump_Color_Mode(*cm));
  288.         
  289.     Get_Flash_Mode(fm, error_msg);
  290.     if (*error_msg!=0) return;
  291.     printf("Flash_Mode=%s\n", dump_Flash_Mode(*fm));
  292.  
  293.     Get_Num_of_Frames_Taken(noft, error_msg);
  294.     if (*error_msg!=0) return;
  295.     printf("Number of Frames Taken=%d\n", *noft);
  296.     max=*noft;
  297.         
  298.     Get_Num_of_Frames_Left(nofl, error_msg);
  299.     if (*error_msg!=0) return;
  300.     printf("Number of Frames Left=%d\n", *nofl);
  301.         
  302.     Get_Battery_Capacity(bc, error_msg);
  303.     if (*error_msg!=0) return;
  304.     printf("Battery Capacity=%d\n", *bc);
  305.         
  306.     /* Get_Comm_Speed(ccs, error_msg);
  307.        if (*error_msg!=0) return;
  308.       printf("Comm Speed=%s\n", dump_Comm_Speed(*ccs));
  309.     */
  310.         
  311.     /* Get_Bright_And_Contrast(brc, error_msg);
  312.       if (*error_msg!=0) return;
  313.      printf("Bright and Contrast=%s\n", dump_Bright_Contrast(*brc));     
  314.    */
  315.  
  316.     Get_White_Balance(wb, error_msg);
  317.     if (*error_msg!=0) return;
  318.     printf("White Balance=%s\n", dump_White_Balance(*wb));
  319.     
  320.     Get_Autoshut_on_Host_Timer(aoht, error_msg);
  321.     if (*error_msg!=0) return;
  322.     printf("Autoshut on Host Timer=%d\n", *aoht);
  323.         
  324.     Get_Autoshut_on_Field_Timer(asoft, error_msg);
  325.     if (*error_msg!=0) return;
  326.     printf("Autoshut on Field Timer=%d\n", *asoft);
  327.     
  328.     Get_Available_Memory_Left(aml, error_msg);
  329.     if (*error_msg!=0) return;
  330.     printf("Available Memory Left=%d [Bytes]\n", *aml);
  331.         
  332.     Get_Lens_Mode(lm, error_msg);
  333.     if (*error_msg!=0) return;
  334.     printf("Lens Mode=%s\n", dump_Lens_Mode(*lm));
  335.         
  336.     Get_LCD_Brightness(lb, error_msg);
  337.     if (*error_msg!=0) return;
  338.     printf("LCD Brightness=%d\n", *lb);
  339.         
  340.     Get_LCD_Autoshut_Timer(lat, error_msg);
  341.     if (*error_msg!=0) return;
  342.     printf("LCD Autoshut Timer=%d\n", *lat);
  343.         
  344.     Get_Date_Format(df, error_msg);
  345.     if (*error_msg!=0) return;
  346.     printf("Date Format=%s\n", dump_Date_Format(*df));
  347.     
  348.     Get_Exp_Meter(em, error_msg);
  349.     if (*error_msg!=0) return;
  350.     printf("Exp Meter=%s\n", dump_Exp_Meter(*em));
  351.         
  352.     /* Get_Optical_Mode(AEL_WBL, Fisheye, Wide, Digital_Zoom, BAW, error_msg);
  353.       if (*error_msg!=0) return;
  354.        printf("AEL_WB=%dL, Fisheye=%d, Wide=%d, Digital_Zoom=%d, BAW=%d\n", *AEL_WBL, *Fisheye, *Wide, *Digital_Zoom, *BAW);
  355.     */
  356.  
  357.     Get_Camera_ID(buf, NULL, error_msg);
  358.     if (*error_msg!=0) return;
  359.     printf("Camera ID=%s\n", (char *) buf);
  360.         
  361.     Get_Serial_Number(buf, NULL, error_msg);
  362.     if (*error_msg!=0) return;
  363.     printf("Serial Number=%s\n", buf);
  364.         
  365.     Get_Version(buf, NULL, error_msg);
  366.     if (*error_msg!=0) return;
  367.     printf("Version=%s\n", buf);
  368.         
  369.     Get_Model((char *) buf, NULL, error_msg);
  370.     if (*error_msg!=0) return;
  371.     printf("Model=%s\n", buf);
  372.  
  373.     /* Get_Camera_Summery_Data(re, LCD_Brightness, Frames_Taken, error_msg);
  374.        if (*error_msg!=0) return;
  375.        printf("Resolution=%s, LCD Brightness=%d, Frames Taken=%d\n", dump(*re), *LCD_Brightness, *Frames_Taken);
  376.     */
  377.          
  378.     Get_Manufacturer((char *) buf, NULL, error_msg);
  379.     if (*error_msg!=0) return;
  380.     printf("Manufacturer=%s\n", buf);
  381.  
  382.     if (max>0) {
  383.         i=1;
  384.         printf("There are %d frames taken\n", max);
  385.         while (i<=max && i<255) {
  386.             printf("Frame[%d]:\n", i);
  387.             Get_Length_of_Frame(i, locf, error_msg);
  388.             if (*error_msg!=0) return;
  389.             printf("   Length=%d\n", *locf);
  390.         
  391.             Get_Length_of_Thumbnail(i, loct, error_msg);
  392.             if (*error_msg!=0) return;
  393.             printf("   Length of Thumbnail=%d\n", *loct);
  394.                 Get_Protect_State_of_Frame(i, psof, error_msg);
  395.             if (*error_msg!=0) return;
  396.             printf("   Protection=%d\n", *psof);
  397.             
  398.             Get_Picture_Summery_Data(i, NULL, NULL, NULL, TimeDate, error_msg);
  399.             if (*error_msg!=0) return;
  400.             printf("   time stamp=%s\n", asctime(localtime(TimeDate)));
  401.                 i++;
  402.         }
  403.     } else printf("There are no pictures in the camera\n");
  404. }
  405.  
  406.  
  407. void do_f(int user_min, int user_max, char *picture_name, long speed, enum Camera_Speed cs, int action, char* error_msg) {
  408.     time_t t;
  409.     time_t time1, time2;
  410.     char name[255], comment[80];
  411.     int i, max, passedtime, NoFT;
  412.     long size;
  413.     struct tm *tm_s;
  414.     
  415.     if ((user_min<1) || (user_max<1) || (user_min>user_max)) {
  416.         printf("Can't accept such ranges\n");
  417.         return;
  418.     }
  419.     
  420.     printf("user_min=%d\n", user_min);
  421.  
  422.     init(cs, error_msg);
  423.     if (*error_msg!=0) return;
  424.  
  425.     Get_Num_of_Frames_Taken(&NoFT, error_msg);
  426.     if (*error_msg!=0) return;
  427.     
  428.     max=(int) NoFT;
  429.     if (max==0) {
  430.         printf("No pictures are stored in the camera.\n");
  431.         return;
  432.     }
  433.  
  434.     if (max<user_min) {
  435.         printf("There are %d pictures in the camera, not one more.\n", max);
  436.         printf("user_min=%d\n", user_min);
  437.         return;
  438.     }
  439.  
  440.     if (max<user_max) user_max=max;
  441.     
  442.     switch(action) {
  443.         case 0:
  444.             printf("Will get thumbnails %d to %d\n", user_min, user_max);
  445.             break;
  446.         case 1: 
  447.             printf("Will get pictures %d to %d\n", user_min, user_max);
  448.             break;
  449.         case 2: 
  450.             printf("Will delete pictures %d to %d\n", user_min, user_max);
  451.             break;
  452.         default:
  453.             printf("action has a wrong value %d\n", action);
  454.     }
  455.     
  456.     for(i=user_min; i<=user_max; i++) {
  457.     
  458.         if (action==0) {
  459.             sprintf(name, "%s_%d.jpeg", picture_name, i);
  460.             Get_Length_of_Thumbnail(i, &size, error_msg);
  461.             if (*error_msg!=0) return;
  462.             printf("Get thumbnail %d, size %d bytes, transfer time: about %d s\n", i, size, (int) 3+size/(speed/10));
  463.         }
  464.     
  465.         if (action==1) {
  466.             sprintf(name, "%s_%d.jpeg", picture_name, i);        
  467.             Get_Length_of_Frame(i, &size, error_msg);
  468.             if (*error_msg!=0) return;
  469.             printf("Get picture %d, size %d bytes, transfer time: about %d s\n", i, size, (int) 3+size/(speed/10));
  470.         }
  471.  
  472.         Get_Picture_Summery_Data(i, NULL, NULL, NULL, &t, error_msg);
  473.         if (*error_msg!=0) return;
  474.             
  475.         tm_s = localtime(&t);
  476.         if (((tm_s->tm_year)<95) && ((tm_s->tm_year)>105)) strcpy(comment, "no date set");
  477.         else strftime(comment, 80, "%d-%b-%Y %H:%M:%S", tm_s );
  478.     
  479.         time(&time1);
  480.         switch (action) {
  481.             case 0:
  482.                 save_Thumbnail(i, name, comment, error_msg);
  483.                 if (*error_msg!=0) return;
  484.                 break;
  485.             case 1:
  486.                 save_Frame(i, name, comment, error_msg);
  487.                 if (*error_msg!=0) return;
  488.                 break;
  489.             case 2:
  490.                 SetIntegerRegister(Cmd_Set_Current_Frame_Number, (L1) i, error_msg);
  491.                 if (*error_msg!=0) return;
  492.                 TakeAction(AC_Erase_Current_Frame, 0, error_msg);
  493.                 if (*error_msg!=0) return;
  494.                 break;
  495.             default:
  496.                 printf("action has a wrong value %d\n", action);
  497.         }                
  498.         time(&time2);
  499.         passedtime=(int) time2-time1;
  500.         if ((action==0) || (action==1)) printf("%s, size %d, transfered in %d s, %5.0f cps\n", name, size, passedtime, ((float) size) / ((float) passedtime) );
  501.     } // end for
  502. }
  503.  
  504.  
  505. void preview_f(enum Camera_Speed cs, char* error_msg) {
  506.     struct JPEGDecHandle *jph;
  507.     struct Screen *scr;
  508.     struct Window *win;
  509.     struct Message *msg;
  510.     UBYTE *buffer=NULL;
  511.     UBYTE *frame_buffer=NULL;
  512.     ULONG DisplayID;
  513.     UWORD DisplayDepth;
  514.     struct ScreenModeRequester *smr;
  515.  
  516.     int err;
  517.     ULONG x, y, count, bpp;
  518.     UBYTE colorspace;
  519.     long *size=0;
  520.     BOOL r;
  521.         
  522.  
  523.     /* Open Libraries */
  524.     
  525.     JpegBase = OpenLibrary( "jpeg.library", NULL );
  526.     if (JpegBase==NULL) {
  527.         printf("Couldn't open jpeg.library\n");
  528.         return;
  529.     }
  530.     
  531.     CyberGfxBase = OpenLibrary( "cybergraphics.library", NULL );
  532.     if (JpegBase==NULL) {
  533.         printf("Couldn't open cybergraphics.library\n");
  534.         goto end5;
  535.     }
  536.     
  537.     /* Open ASL and ask user about screen */
  538.     
  539.     smr = (struct ScreenModeRequester* ) AllocAslRequestTags(ASL_ScreenModeRequest,
  540.         ASLSM_MinDepth, (ULONG) 15,
  541.         TAG_DONE);
  542.         
  543.     if (smr==NULL) {
  544.         printf("Couldn't do an AllocAslRequestTags()\n");
  545.         goto end4;
  546.     }
  547.     
  548.     r = AslRequest(smr, TAG_DONE);
  549.     if (!r) {
  550.         FreeAslRequest(smr);
  551.         printf("User did nothing?\n");
  552.         goto end4;
  553.     }
  554.     
  555.     DisplayID=smr->sm_DisplayID;
  556.     DisplayDepth=smr->sm_DisplayDepth;
  557.     FreeAslRequest(smr);
  558.     
  559.     /* Open Screen and Window */
  560.     
  561.     scr = OpenScreenTags( NULL,
  562.                                 SA_Title, "Camedia_Snapshot",
  563.                                 //SA_DisplayID, 0x40120051,
  564.                                 SA_DisplayID, DisplayID,
  565.                                 //SA_Depth, GetCyberIDAttr( CYBRIDATTR_DEPTH, 0x40120051),
  566.                                 SA_Depth, DisplayDepth,
  567.                                 TAG_DONE );
  568.     if (scr==NULL) {
  569.         printf("Couln't open screen\n");
  570.         goto end4;
  571.     }
  572.     
  573.     win = OpenWindowTags( NULL,
  574.                                 WA_Title, "Preview",                                
  575.                                 WA_Flags, WFLG_ACTIVATE | WFLG_SIMPLE_REFRESH |
  576.                                 WFLG_SIZEGADGET | WFLG_RMBTRAP | WFLG_DRAGBAR |
  577.                                 WFLG_DEPTHGADGET | WFLG_CLOSEGADGET,
  578.                                 WA_IDCMP, IDCMP_CLOSEWINDOW,
  579.                                 WA_Left, 0,
  580.                                 WA_Top, 0,
  581.                                 WA_Width, 500,
  582.                                 WA_Height, 380,
  583.                                 WA_MinWidth, 100,
  584.                                 WA_MinHeight, 70,
  585.                                 WA_MaxWidth, scr->Width,
  586.                                 WA_MaxHeight, scr->Height,
  587.                                 WA_AutoAdjust, 1,
  588.                                 WA_CustomScreen, scr,
  589.                                 TAG_DONE );
  590.  
  591.     if ( win == NULL ) {
  592.         printf("Couldn't open window\n");
  593.         goto end3;
  594.     }
  595.     
  596.     buffer=(UBYTE *) AllocMem(256000+6000, MEMF_CLEAR);
  597.     if (buffer==NULL) {
  598.         printf("No memory\n");
  599.         goto end2;
  600.     }
  601.     frame_buffer=buffer+256000;
  602.     
  603.     /* retrieve previews */
  604.     
  605.     init(cs, error_msg);
  606.     if (*error_msg!=0) {
  607.         printf("Error in init() %s\n", error_msg);
  608.         goto end2;
  609.     }
  610.     
  611.     for (;;) {
  612.         msg=GetMsg( win->UserPort );
  613.         if (msg!=NULL) {
  614.             ReplyMsg(msg);
  615.             while ( ( msg = GetMsg( win->UserPort ) ) != NULL ) ReplyMsg( msg );
  616.             goto end1;
  617.         }
  618.         
  619.         Get_Preview((UBYTE *) frame_buffer, size, error_msg);
  620.         if (*error_msg!=0) {
  621.             printf("Error in Get_Preview() %s\n", error_msg);
  622.             goto end1;
  623.         }
  624.  
  625.         /* Decode picture */
  626.         
  627.         err = AllocJPEGDecompress( &jph,
  628.                 JPG_SrcMemStream, frame_buffer,
  629.                 JPG_SrcMemStreamSize, *size,
  630.                 TAG_DONE );
  631.  
  632.         if (err!=0) {
  633.             printf("error in AllocJPEGDecompress\n");
  634.             goto end1;
  635.         }
  636.         err = GetJPEGInfo( jph,
  637.                                 JPG_Width, &x, JPG_Height, &y,
  638.                                 JPG_ColourSpace, &colorspace, 
  639.                                 JPG_BytesPerPixel, &bpp,
  640.                                 JPG_ScaleNum, 1, JPG_ScaleDenom, 1,
  641.                                 TAG_DONE );
  642.         
  643.         if (err!=0) {
  644.             printf("error in GetJPEGInfo\n");
  645.             FreeJPEGDecompress(jph);
  646.             goto end1;
  647.         }
  648.  
  649.         err = DecompressJPEG( jph,
  650.                                     JPG_DestRGBBuffer, buffer,
  651.                                     JPG_ScaleNum, 1, JPG_ScaleDenom, 1,
  652.                                     TAG_DONE );
  653.         if (err!=0) {
  654.             printf("error in DecompressJPEG(...)\n");
  655.             FreeJPEGDecompress(jph);
  656.             goto end1;
  657.         }
  658.     
  659.         /* Display picture */
  660.         count= (ULONG) ScalePixelArray(buffer, x, y, (bpp*x), win->RPort, win->BorderLeft, win->BorderTop, win->GZZWidth, win->GZZHeight, RECTFMT_RGB);
  661.  
  662.         FreeJPEGDecompress(jph);
  663.     }
  664.     
  665.     /* Close Windows and Screen*/
  666.  
  667. end1:
  668.     FreeMem(buffer, 256000+6000);
  669. end2:
  670.     CloseWindow(win);
  671. end3:
  672.     CloseScreen(scr);
  673. end4:
  674.     /* Close Libraries */
  675.     CloseLibrary(CyberGfxBase);
  676. end5:
  677.     CloseLibrary(JpegBase);
  678. }